import re
import sys

def count_chars(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return len(f.read())
    except Exception as e:
        return f"错误: {e}"

def count_words(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
            # 修正正则：允许单词中包含撇号（'），但必须以字母开头和结尾
            words = re.findall(r"\b[a-zA-Z]+(?:['][a-zA-Z]+)*\b", content)
            return len(words)
    except Exception as e:
        return f"错误: {e}"

def count_sentences(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
            sentences = re.split(r'[.!?]+', content)
            return len([s for s in sentences if s.strip()])
    except Exception as e:
        return f"错误: {e}"

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("命令格式：python wc.py [-c|-w|-s] [文件名]")
        sys.exit(1)
    
    param = sys.argv[1]
    file_name = sys.argv[2]
    
    if param == '-c':
        print(f"字符数: {count_chars(file_name)}")
    elif param == '-w':
        print(f"单词数: {count_words(file_name)}")
    elif param == '-s':
        print(f"句子数: {count_sentences(file_name)}")
    else:
        print("支持的参数：-c（字符）、-w（单词）、-s（句子）")